//
//  LiuEmoticonToolBarView.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/4/6.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit

enum LiuEmoticonType: Int {
    //  最近
    case recent = 10
    //  默认
    case normal = 11
    //  emoji
    case emoji = 12
    //  浪小花
    case lxh = 13
}

class LiuEmoticonToolBarView: UIStackView {
    
    //回调的闭包
    var buttonActionClosure: ((LiuEmoticonType)->())?

    fileprivate var lastButton:UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setupUI() {
        //添加4个按钮
        addchildButton(imageName: "compose_emotion_table_left", title: "最近", type: .recent)
        addchildButton(imageName: "compose_emotion_table_mid", title: "默认", type: .normal)
        addchildButton(imageName: "compose_emotion_table_mid", title: "emoji", type: .emoji)
        addchildButton(imageName: "compose_emotion_table_right", title: "浪小花", type: .lxh)
        
        //均分
        distribution = .fillEqually
        
    }
    
    //加载按钮
    fileprivate func addchildButton(imageName:String ,title:String ,type:LiuEmoticonType) {
        
        let button = UIButton()
        button.tag = type.rawValue
        button.setBackgroundImage(UIImage(named:imageName + "_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named:imageName + "_selected"), for: .selected)
        button.setTitle(title, for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        //取消按钮的选中变色状态
        button.adjustsImageWhenHighlighted = false
        
        //添加点击事件
        button.addTarget(self, action: #selector(buttonAction(btn:)), for: .touchUpInside)
        
        //添加按钮
        addArrangedSubview(button)
    }
    
    //按钮点击
    @objc fileprivate func buttonAction(btn:UIButton) {
        
        //如果上一次的按钮和这次的相同,就停止
        if lastButton == btn {
            return
        }
        
        //上一次的进入normal状态
        lastButton?.isSelected = false
        
        //这次的按钮进入选中状态
        btn.isSelected = true
        
        //记录新的按钮
        lastButton = btn
        
        //获取枚举值
        let type:LiuEmoticonType = LiuEmoticonType(rawValue: btn.tag)!
        
        //回调type
        buttonActionClosure?(type)
        
    }
    
    //给外界提供一个方法,改变按钮的选中状态
    func changeButtonType(section:Int) {
        //根据section获取按钮
        let  button:UIButton = viewWithTag(section + 10) as! UIButton
        
        //判断和上次的按钮是否为同一个
        if lastButton == button {
            return
        }
        
        //上一次的进入normal状态
        lastButton?.isSelected = false
        
        //这次的按钮进入选中状态
        button.isSelected = true
        
        //记录新的按钮
        lastButton = button
        
        
    }

}
